package com.chen.invoicing;

import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.swift.cfg4u.instance.ConstInstance;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@SuppressWarnings({ "static-access", "unchecked" })
public class RedisService {
	static JedisPool jedisPool = null;

	public synchronized static void init() {
//		String ip = ConstInstance.getValuedByKeyd("redis", "ip");
//		int port = Integer.valueOf(ConstInstance.getValuedByKeyd("redis", "port"));
//		String passwd = ConstInstance.getValuedByKeyd("redis", "passwd");
//		int maxActive =Integer.valueOf(ConstInstance.getValuedByKeyd("redis", "maxActive"));
//		int maxIdle = Integer.valueOf(ConstInstance.getValuedByKeyd("redis", "maxIdle"));
//		int maxWait = Integer.valueOf(ConstInstance.getValuedByKeyd("redis", "maxWait"));
//		boolean testOnBorrow = Boolean.valueOf(ConstInstance.getValuedByKeyd("redis", "testOnBorrow"));
//		int maxTimeout = Integer.valueOf(ConstInstance.getValuedByKeyd("redis", "maxTimeout"));
		
		String ip = "192.168.0.248";
		int port = 10000;
		String passwd = "acGrfP9omb1EGMzCUazC";
		int maxActive = 10000;
		int maxIdle = 10000;
		int maxWait = 10000;
		boolean testOnBorrow = false;
		int maxTimeout = 10000;

		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(maxActive);
		config.setMaxIdle(maxIdle);
		config.setMaxWait(maxWait);
		config.setTestOnBorrow(testOnBorrow);
		if (!StringUtils.isEmpty(passwd))
			jedisPool = new JedisPool(config, ip, port, maxTimeout, passwd);
		else
			jedisPool = new JedisPool(config, ip, port, maxTimeout);

	}

	public static long zadd(String key, long score, String value) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			return jedis.zadd(key, score, value);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return 0;
	}

	public static long zremove(String key, String member) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			return jedis.zrem(key, member);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return 0;
	}

	public static long zscore(String key, String member) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			Double score = jedis.zscore(key, member);
			if (score != null)
				return score.longValue();
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return 0;
	}

	public static Set<String> zrangebyscore(String key, long min, long max) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
		
			return jedis.zrangeByScore(key, min, max);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return null;
	}

	
	public static Set<String> zrangebyscore(String key, long min, long max,int offset,int count) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
		
			return jedis.zrangeByScore(key, min, max, offset, count);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return null;
	}
	
	
	public static String get(String key) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			return jedis.get(key);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return null;
	}

	public static String set(String key, String value) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			return jedis.set(key, value);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return null;
	}

	public static void del(String key) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			jedis.del(key);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
	}

	public static long hlen(String key) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			return jedis.hlen(key);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}

		return 0;
	}

	public static String hget(String key, String field) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			return jedis.hget(key, field);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}

		return null;
	}

	public static Map<String, String> hgetAll(String key) {
		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			return jedis.hgetAll(key);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}

		return null;
	}

	public static void hdel(String key, String field) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			jedis.hdel(key, field);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}

	}

	public static void hset(String key, String field, String value) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			jedis.hset(key, field, value);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}

	}

	public static void lpush(String listkey, String value) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			jedis.lpush(listkey, value);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}

	}

	public static String rpop(String listkey) {

		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			return jedis.rpop(listkey);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return null;

	}
	/**
	 * 程序计数器，递增
	 *<br />
	 *@date 2015-5-25 下午7:49:38
	 *@author <a href="bentengwu@163.com">伟宏</a>
	 *@param key	需要被递增的key
	 *@return 递增后的数值
	 *		 如果key的值为null,则返回null
	 *		 如果异常,返回null.
	 *@since 2.0.2
	 */
	
	public static Long incrKey(String key)
	{
		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			return jedis.incr(key);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return null;
	}
	
	public static Long decrKey(String key)
	{
		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			return jedis.decr(key);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return null;
	}
	
	/**
	 * 
	 *@描述  ：设置key的实效
	 *@创建人：zhongy
	 *@创建时间：2016年7月25日 下午6:09:58
	 *@修改人：
	 *@修改时间：
	 *@修改描述：
	 *@param key
	 *@param seconds
	 *@return
	 */
	public static  Long  setExpireWithPrefix(String key, int seconds)
	{
		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			return jedis.expire(key, seconds);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return null;
	}
	
	
	
	/**
	 * 
	 *@描述  ：获取key时效
	 *@创建人：zhongy
	 *@创建时间：2016年7月25日 下午7:01:27
	 *@修改人：
	 *@修改时间：
	 *@修改描述：
	 *@param key
	 *@return 
	 *如果该key不存在，返回-2；
	 *如果该key未设置存活时间，返回-1；
	 *如果设置过存活时间，则返回剩余的存活秒数
	 */
	public static  Long  ttl(String key)
	{
		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			return jedis.ttl(key);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return null;
	}
	
	/**
	 * 
	 *@描述  ：将给定 key 的值设为 value ，并返回 key 的旧值(old value)。当 key 存在但不是字符串类型时，返回一个错误。
	 *@创建人：zhongy
	 *@创建时间：2016年8月9日 下午7:35:41
	 *@修改人：
	 *@修改时间：
	 *@修改描述：
	 *@param key
	 *@param value
	 *@return
	 */
	public static  String  getSet(String key,String value)
	{
		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			return jedis.getSet(key, value);
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return null;
	}
	
	
	public static  boolean  setAndexpire(String key,String value, int seconds)
	{
		Jedis jedis = null;
		boolean borrowOrOprSuccess = true;
		try {
			if (jedisPool == null) {
				init();
			}
			jedis = jedisPool.getResource();
			//setp1、设置key
			String ok=jedis.set(key, value);
			if(!"OK".equalsIgnoreCase(ok)){
				return false;
			}
			//step2、设置key的有效期
			Long s=jedis.expire(key, seconds);
			if(s==null||s.longValue()!=1){//失败
				jedis.del(key);//删除原来的key
				return false;
			}
			return true;
		} catch (Exception e) {
			borrowOrOprSuccess = false;
			e.printStackTrace();
			if (jedis != null)
				jedisPool.returnBrokenResource(jedis);
		} finally {
			if (borrowOrOprSuccess)
				jedisPool.returnResource(jedis);
		}
		return false;
	}
	
	
	
	
	
	
	static public void main(String[] args) throws InterruptedException {
		org.swift.cfg4u.servlet.ConfigController.start();
//		String ip = ConstInstance.getValuedByKeyd("redis", "ip");
//		int port = Integer.valueOf(ConstInstance.getValuedByKeyd("redis", "port"));
//		String passwd = ConstInstance.getValuedByKeyd("redis", "passwd");
//
//		Jedis j = new Jedis(ip, port);
//		j.auth(passwd);
//		String key="ceshi";
//		j.set(key, "测试");
//		setExpireWithPrefix("ceshi", 1000);
//		while(true){
//			System.out.println(ttl(key));
//			Thread.sleep(1000l);
//		}
//		
//		String key="shanghai_orderId";
//		System.out.println(getSet(key, "1"));
//		setExpireWithPrefix(key, 10);
//		
//		System.out.println(get(key));

		String key="13588103044";
		System.out.println(RedisService.incrKey(key));
		
		
		
		
		
		
		
		
		
//		System.out.println(j.hget("hkey_dest_part", "1358846"));
//
//		System.out.println(hget("hkey_dest_part", "1358846"));
	}

}
